// 导入包
import WxmpRsa from "wxmp-rsa";
import dayjs from "dayjs";

// 密钥对生成 http://web.chacuo.net/netrsakeypair

const publicKey = `MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEA02HzJQU4PkGwjr9MStvG
  3hx5PR79CjxMi3OCn+yhA4pT20f+yqAPNdjdztO75bO+eMPYUNp+MCuYMFpHWV0S
  kYldfBb25rA5dSoUdAWoOdsZ2veN1A1fxyw6AWW8lICUeUMzdQAzoED0Cz4srD3M
  AaNVmLX6+jEX364hDxhNB5eWUV/W9mGO0+RVcn+FM2XKm5J7iajQQ6WkQEn34tnA
  HWDzTPauCQJFgEIFBhUCUqkWxmJ/0jMWPWAjSxntk/xX/j9I1lobJXk9dUi6iOvz
  dTTbbZl+YJCw44Q1XVq+dqI5P4kqgH9xMvZAahW5bjbUq1ttupK6dg+YwemyBe/i
  Fe3hMUU/lcwEM8Yc+HFB4a9RSYSQhfPUaEL3qcsAmOt4YUSQhTm3GCTsrwGZXjej
  vV+DeEMglYY4nxTweEtN3PB71ZNUDiXd2ENbWzjJ22bFk4Je5drAGBsH5R9HTOJB
  efXAEyrEjn3GVt6+eakysO2ckK9RnhxzDWMrUrns7mq/u1GX7I19ulDTJXFPnWK3
  nXs4tluGDIaRizdWw4ehVwtlCuOCPIKD5yJggBJHRySGRtTRVXiZLjxUJ4zX2VnF
  co796EnJ2tlnrZwizGLCocIXPfEC/6os0R6q7MCW/7r0Gdr2+Pq8+hYMf4XbQI0J
  +8vVUd4kymioN3A4AZCTu0cCAwEAAQ==`;

// 实例化rsa
const rsa = new WxmpRsa();

// 加密
// export function encrypt(txt) {
//   rsa.setPublicKey(publicKey); // 设置公钥
//   return rsa.encryptLong(txt);
// }

/**
 * 加密
 * @param {*} jsonData json数据
 */
export function encrypt(jsonData) {
  rsa.setPublicKey(publicKey); // 设置公钥
  // 加上当前时间戳
  const timestamp = dayjs().valueOf();
  jsonData["timestamp"] = timestamp;
  return rsa.encryptLong(JSON.stringify(jsonData));
}
